﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using WLMCoreLibrary;
using SAW_Core;
using System.Globalization;


namespace SAW_Mobile.Facturacion
{
    public partial class Detalle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            dbConnector ConexionDB = new dbConnector();
            int idFactura = Convert.ToInt32(Request.QueryString["idFactura"]);
            Factura factura = new Factura(idFactura,ref ConexionDB);
            FacturasitemsCollection lista = new FacturasitemsCollection(factura, ref ConexionDB);
            StringBuilder salida = new StringBuilder();
            FolioFactura.Text = factura.Serie + factura.NoFolio;
            string direccion = factura.R_Calle + " #" + factura.R_NumeroExterior + " " + factura.R_NumeroInterior + "<br/>" +
                factura.R_Colonia + " <br/>" +
                factura.R_Municipio + ", " + factura.R_Estado + ", " + factura.R_CP;
            DatosFacturacion.Text = "<div>"+factura.fechaCreacion.ToLongDateString()+"<br><b>" + factura.R_RFC + "<br>" + factura.R_RazonSocial.ToLower(new CultureInfo("es-MX")) + "</b><br/>" +
               direccion.ToLower()
                +"<br><br></div>";
            salida.Append("<ul data-role='listview'><li data-role='list-divider'  data-theme='a'>Conceptos</li>");
            //salida.Append("<li data-role=\"list-divider\">" + letraActual + "</li>");
            foreach (Facturasitems item in lista.Elementos)
            {

                salida.Append("<li><p>" + item.Descripcion.ToLower() + "<br>" + item.cantidad + "<br>" + item.Precio.ToString("C") + "</p><span class='ui-li-count ui-btn-up-c ui-btn-corner-all'>" + (item.cantidad * item.Precio).ToString("C") + "</span></li>");
            }
            salida.Append("<li data-role=\"list-divider\">Sub Total<span class='ui-li-count ui-btn-up-c ui-btn-corner-all'>" + factura.SubTotal.ToString("C") + "</span></li>");
            salida.Append("<li >Descuento<span class='ui-li-count ui-btn-up-c ui-btn-corner-all'>" + factura.Descuento.ToString("C") + "</span></li>");
            salida.Append("<li >IVA<span class='ui-li-count ui-btn-up-c ui-btn-corner-all'>" + ((factura.SubTotal-factura.Descuento) * (factura.TasaImpuesto / 100)).ToString("C") + "</span></li>");
            salida.Append("<li data-role=\"list-divider\" data-theme='e'><h3>Total</h3><span class='ui-li-count ui-btn-up-c ui-btn-corner-all totales'>" + factura.Total.ToString("C") + "</span></li>");
            salida.Append("</ul>");
            ListaConceptos.Text = salida.ToString();

            string toEncode = "http://saw.wlm.com.mx/printInvoice.aspx?idFactura=" + factura.id;
            byte[] toEncodeAsBytes= System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
            string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);

            string urlPrint = "http://pdfy.me/g/"+returnValue+"/36a26739836f10eae93dcd19a6dc9d815ab26c8e/";
            printButton.Text = "<a href='" + urlPrint + "' data-role='button' target='_blank' data-icon=\"check\">Generar PDF</a>";
            ConexionDB.CloseConnection();
        }
    }
}